from Case.python_ui.Common.basepage import Basepage
import time, pytest
from Case.python_ui.PageLocate.admin_coupon_voucher_template_locator import AdminCouponVouchertemplateLocator
from Case.python_ui.Common.commontool import CommonTool
from selenium.webdriver.common.action_chains import ActionChains
from Case.python_ui.PageLocate.admin_coupon_promo_crowd_locator import AdminCouponPromoCrowdLocator
from Case.python_ui.Pageobject.admin_coupon_promo_crowd_page import AdminCouponPromoCrowdPage
from Case.python_ui.PageLocate.admin_coupon_coupon_records_locator import AdminCouponCouponRecordsLocator
from Case.python_ui.PageLocate.admin_coupon_voucher_records_locator import AdminCouponVoucherRecordsLocator
from selenium.common.exceptions import NoSuchElementException
import datetime


class VoucherTemplatePage(Basepage):

    #判断是否包含元素
    def check_contain_str(self, string, except_string):
        if except_string in string:
            assert True
        else:
            assert False , '实际值:' + string + '期望值:' + except_string

    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result

    # 元素是否存在
    def element_is_exist(self, locate):
        doc = '判断元素是否存在'
        try:
            self.get_element(locate, doc)
        except NoSuchElementException as e:
            return False
        else:
            return True

    #进入Voucher Template 菜单页面
    def enter_voucher_template_page(self):
        time.sleep(2)
        doc = "进入Voucher Template 菜单页面"
        js_object = 'document.getElementsByClassName("cre-sider-menu cre-sider-menu-open")[0].scrollTop=10000'
        self.excute_js(js_object)
        self.wait_elevisble(AdminCouponVouchertemplateLocator.promo_management_menu, doc)
        self.click_element(AdminCouponVouchertemplateLocator.promo_management_menu, doc)
        time.sleep(2)
        self.wait_elevisble(AdminCouponVouchertemplateLocator.voucher_template_menu, doc)
        self.click_element(AdminCouponVouchertemplateLocator.voucher_template_menu, doc)
        time.sleep(3)


    # 创建重复使用的 voucher template
    def create_reuse_voucher_template(self):
        time.sleep(2)
        voucher_template_name = 'lwm_auto' + CommonTool.generate_edit_role_name()
        doc = "创建重复使用的Voucher Template"
        # 点击创建按钮
        self.wait_elevisble(AdminCouponVouchertemplateLocator.create_voucher_template_button, doc)
        self.click_element(AdminCouponVouchertemplateLocator.create_voucher_template_button, doc)
        time.sleep(2)
        # 输入template 名称
        self.wait_elevisble(AdminCouponVouchertemplateLocator.voucher_template_input, doc)
        self.send_keys(AdminCouponVouchertemplateLocator.voucher_template_input, voucher_template_name, doc)
        time.sleep(2)
        #输入固定金额
        fixed_amount = '2000'
        self.send_keys(AdminCouponVouchertemplateLocator.fixed_amount_input, fixed_amount, doc)
        time.sleep(2)
        # 打开reuse 开关
        self.click_element(AdminCouponVouchertemplateLocator.reuse_button, doc)
        time.sleep(2)
        # 选择起始时间
        self.click_element(AdminCouponVouchertemplateLocator.start_time_input, doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.start_time, doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.end_time, doc)
        time.sleep(2)
        # 点击提交
        self.click_element(AdminCouponVouchertemplateLocator.submit_button, doc)
        time.sleep(5)
        # 检查创建结果
        self.check_create_voucher_template_result(voucher_template_name)
        self.check_list_attribute()
        #检查reuse字段
        doc = '检查reuse字段'
        except_result = 'Y'
        self.check_attribute(self.get_element(AdminCouponVouchertemplateLocator.first_search_result_reuse, doc).text, except_result)

    # 创建固定日期voucher template
    def create_fixed_date_voucher_template(self):
        time.sleep(2)
        voucher_template_name = 'lwm_auto' + CommonTool.generate_edit_role_name()
        doc = "创建固定日期voucher template"
        # 点击创建按钮
        self.wait_elevisble(AdminCouponVouchertemplateLocator.create_voucher_template_button, doc)
        self.click_element(AdminCouponVouchertemplateLocator.create_voucher_template_button, doc)
        time.sleep(2)
        # 输入template 名称
        self.wait_elevisble(AdminCouponVouchertemplateLocator.voucher_template_input, doc)
        self.send_keys(AdminCouponVouchertemplateLocator.voucher_template_input, voucher_template_name, doc)
        time.sleep(2)
        # 修改货币单位
        if self.get_element(AdminCouponVouchertemplateLocator.currency_button, doc).text != 'IDR':
            self.click_element(AdminCouponVouchertemplateLocator.currency_button, doc)
            time.sleep(2)
            self.click_element(AdminCouponVouchertemplateLocator.currency_IDR, doc)
            time.sleep(2)
        # 输入固定金额
        fixed_amount = '2000'
        self.send_keys(AdminCouponVouchertemplateLocator.fixed_amount_input, fixed_amount, doc)
        time.sleep(2)
        # 选择起始时间
        start_time = datetime.datetime.now()
        end_time = start_time + datetime.timedelta(days=5)
        self.send_keys(AdminCouponVouchertemplateLocator.start_time_input, start_time.strftime("%d-%m-%Y"), doc)
        time.sleep(2)
        self.send_keys(AdminCouponVouchertemplateLocator.end_time_input, end_time.strftime("%d-%m-%Y"), doc)
        time.sleep(2)
        #enter 确认
        self.keyboard_event_enter(AdminCouponVouchertemplateLocator.end_time_input, doc)
        time.sleep(2)
        # self.click_element(AdminCouponVouchertemplateLocator.start_time_input, doc)
        # time.sleep(2)
        # self.click_element(AdminCouponVouchertemplateLocator.start_time, doc)
        # time.sleep(2)
        # self.click_element(AdminCouponVouchertemplateLocator.end_time, doc)
        # time.sleep(2)
        # 点击提交
        self.click_element(AdminCouponVouchertemplateLocator.submit_button, doc)
        time.sleep(5)
        # 检查创建结果
        self.check_create_voucher_template_result(voucher_template_name)
        self.check_list_attribute()


    # 创建动态日期voucher template
    def create_dynamic_voucher_template(self):
        time.sleep(2)
        voucher_template_name = 'lwm_auto' + CommonTool.generate_edit_role_name()
        doc = "创建动态日期voucher template"
        # 点击创建按钮
        self.wait_elevisble(AdminCouponVouchertemplateLocator.create_voucher_template_button, doc)
        self.click_element(AdminCouponVouchertemplateLocator.create_voucher_template_button, doc)
        time.sleep(2)
        # 输入template 名称
        self.wait_elevisble(AdminCouponVouchertemplateLocator.voucher_template_input, doc)
        self.send_keys(AdminCouponVouchertemplateLocator.voucher_template_input, voucher_template_name, doc)
        time.sleep(2)
        # 输入固定金额
        fixed_amount = '2000'
        self.send_keys(AdminCouponVouchertemplateLocator.fixed_amount_input, fixed_amount, doc)
        time.sleep(2)
        # 勾选dynamic
        self.click_element(AdminCouponVouchertemplateLocator.validation_type_dynmic, doc)
        time.sleep(2)
        # 点击提交
        self.click_element(AdminCouponVouchertemplateLocator.submit_button, doc)
        time.sleep(5)
        # 检查创建结果
        self.check_create_voucher_template_result(voucher_template_name)
        self.check_list_attribute()
        #检查Validation Period 字段
        doc = '检查Validation Period 字段'
        except_result = '1 Hour after distributed'
        self.check_attribute(self.get_element(AdminCouponVouchertemplateLocator.first_search_result_validation_period, doc).text, except_result)


    # 搜索voucher template
    def query_voucher_template(self):
        time.sleep(2)
        doc = '搜索voucher template'
        fuzzy_query = ['lwm', 'auto']
        # 先创建模板，保证有搜索结果
        self.create_fixed_date_voucher_template()
        time.sleep(2)
        # 模糊查询
        self.wait_elevisble(AdminCouponVouchertemplateLocator.search_input, doc)
        # 清空搜索框
        self.keyboard_event(AdminCouponVouchertemplateLocator.search_input, doc)
        self.send_keys(AdminCouponVouchertemplateLocator.search_input, fuzzy_query[0], doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.search_button, doc)
        time.sleep(5)
        # 检查是否能模糊查询
        self.check_contain_str(self.get_element(AdminCouponVouchertemplateLocator.first_search_result, doc).text,fuzzy_query[0])
        # 清空搜索框
        self.keyboard_event(AdminCouponVouchertemplateLocator.search_input, doc)
        # 模糊查询
        self.send_keys(AdminCouponVouchertemplateLocator.search_input, fuzzy_query[1], doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.search_button, doc)
        time.sleep(5)
        # 检查是否能模糊查询
        self.check_contain_str(self.get_element(AdminCouponVouchertemplateLocator.first_search_result, doc).text, fuzzy_query[1])

    # 编辑voucher template
    def edit_voucher_template(self):
        time.sleep(2)
        doc = '编辑coupon template'
        # 先创建模板，保证有可编辑的模板
        self.create_fixed_date_voucher_template()
        time.sleep(2)
        # 点击编辑按钮
        self.wait_elevisble(AdminCouponVouchertemplateLocator.edit_template_button, doc)
        self.click_element(AdminCouponVouchertemplateLocator.edit_template_button, doc)
        time.sleep(3)
        # 修改名称和金额
        self.wait_elevisble(AdminCouponVouchertemplateLocator.voucher_template_input, doc)
        old_tempalte_name = self.get_element(AdminCouponVouchertemplateLocator.voucher_template_input, '获取原来名字').get_attribute('value')
        new_tempalte_name = old_tempalte_name + '_modify_name'
        new_fixed_amount = '2500'
        self.keyboard_event(AdminCouponVouchertemplateLocator.voucher_template_input, doc)
        self.keyboard_event(AdminCouponVouchertemplateLocator.fixed_amount_input, doc)
        self.send_keys(AdminCouponVouchertemplateLocator.voucher_template_input, new_tempalte_name, doc)
        self.send_keys(AdminCouponVouchertemplateLocator.fixed_amount_input, new_fixed_amount, doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.submit_button, doc)
        time.sleep(5)
        ######检查编辑结果
        self.wait_elevisble(AdminCouponVouchertemplateLocator.search_input, doc)
        self.send_keys(AdminCouponVouchertemplateLocator.search_input, new_tempalte_name, doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.search_button, doc)
        time.sleep(5)
        # 检查修改后的名称是否正确
        assert self.get_element(AdminCouponVouchertemplateLocator.first_search_result, doc).text == new_tempalte_name
        # 检查修改后的金额是否正确
        except_result = '2,500'
        self.check_contain_str(self.get_element(AdminCouponVouchertemplateLocator.first_search_result_fixed_amount, doc).text, except_result)
        # 检查模板历史是否有记录
        doc = '检查模板历史是否有记录'
        self.click_element(AdminCouponVouchertemplateLocator.first_search_result, doc)
        time.sleep(5)
        self.check_attribute(self.get_element(AdminCouponVouchertemplateLocator.first_history_voucher_template, doc).text, old_tempalte_name)

    #复制voucher template
    def copy_voucher_template(self):
        #先创建模板，保证有可复制的模板
        self.create_fixed_date_voucher_template()
        time.sleep(2)
        doc = '复制voucher template'
        # 点击copy按钮
        self.move_mouse_to_click_element(AdminCouponVouchertemplateLocator.actions_button, doc)
        self.wait_elevisble(AdminCouponVouchertemplateLocator.copy_template_button, doc)
        self.click_element(AdminCouponVouchertemplateLocator.copy_template_button, doc)
        time.sleep(5)
        copy_coupon_template = self.get_element(AdminCouponVouchertemplateLocator.voucher_template_input, doc).get_attribute('value')
        #点击submit按钮
        self.wait_elevisble(AdminCouponVouchertemplateLocator.submit_button, doc)
        self.click_element(AdminCouponVouchertemplateLocator.submit_button, doc)
        time.sleep(5)
        #验证是否复制成功
        self.wait_elevisble(AdminCouponVouchertemplateLocator.search_input, doc)
        self.send_keys(AdminCouponVouchertemplateLocator.search_input, copy_coupon_template, doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.search_button, doc)
        time.sleep(5)
        assert self.get_element(AdminCouponVouchertemplateLocator.first_search_result, doc).text == copy_coupon_template

    # generate voucher
    def generate_voucher(self):
        # 先创建模板,先创建模板，保证有可generate的数据
        self.create_fixed_date_voucher_template()
        time.sleep(2)
        doc = 'generate voucher'
        # 获取voucher template的名称
        voucher_template_name = self.get_element(AdminCouponVouchertemplateLocator.search_input, doc).get_attribute('value')
        self.move_mouse_to_click_element(AdminCouponVouchertemplateLocator.actions_button, doc)
        self.wait_elevisble(AdminCouponVouchertemplateLocator.generate_button, doc)
        self.click_element(AdminCouponVouchertemplateLocator.generate_button, doc)
        time.sleep(2)
        # 输入数量
        voucher_number = '12'
        self.wait_elevisble(AdminCouponVouchertemplateLocator.quantity_input, doc)
        self.send_keys(AdminCouponVouchertemplateLocator.quantity_input, voucher_number, doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.generate_button, doc)
        time.sleep(3)
        # 检查generate结果
        # 查询刚分配的voucher template
        doc = '检查generate结果'
        self.send_keys(AdminCouponVouchertemplateLocator.search_input, voucher_template_name, doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.search_button, doc)
        time.sleep(5)
        # 点击详情
        self.click_element(AdminCouponVouchertemplateLocator.first_search_result, doc)
        self.wait_elevisble(AdminCouponVouchertemplateLocator.generate_history_tab, doc)
        self.click_element(AdminCouponVouchertemplateLocator.generate_history_tab, doc)
        # 滚动到底下，校验的返回的数据量是否和创建的一致
        js_down = "window.scrollTo(0,1000)"
        self.excute_js(js_down)
        time.sleep(2)
        search_num = self.get_element(AdminCouponVouchertemplateLocator.search_result_num, doc).text
        assert voucher_number in search_num, '实际值:' + search_num + '期望值:' + voucher_number

        # 检验Voucher Records 页面有优惠券记录
        doc = '检验Voucher Records 页面有优惠券记录'
        # 访问Voucher Records页面
        self.click_element(AdminCouponVoucherRecordsLocator.voucher_records_menu, doc)
        time.sleep(2)
        self.wait_elevisble(AdminCouponVoucherRecordsLocator.search_input, doc)
        self.send_keys(AdminCouponVoucherRecordsLocator.search_input, voucher_template_name, doc)
        time.sleep(2)
        # 点击搜索
        self.click_element(AdminCouponVoucherRecordsLocator.search_button, doc)
        time.sleep(2)
        # 校验的返回的数据量是否和创建的一致
        doc = '校验的返回的数据量是否和创建的一致'
        self.wait_elevisble(AdminCouponVoucherRecordsLocator.search_result_num, doc)
        search_num = self.get_element(AdminCouponVoucherRecordsLocator.search_result_num, doc).text
        assert voucher_number in search_num, '实际值:' + search_num + '期望值:' + voucher_number
        return self.get_element(AdminCouponCouponRecordsLocator.first_search_result, doc).text, voucher_template_name


    # disable voucher template
    def disable_voucher_template(self):
        # 先创建模板，保证有可disabled的模板
        self.create_fixed_date_voucher_template()
        time.sleep(2)
        doc = 'disable coupon template'
        self.move_mouse_to_click_element(AdminCouponVouchertemplateLocator.actions_button, doc)
        self.wait_elevisble(AdminCouponVouchertemplateLocator.disable_button, doc)
        self.click_element(AdminCouponVouchertemplateLocator.disable_button, doc)
        self.wait_elevisble(AdminCouponVouchertemplateLocator.disable_ok_button, doc)
        self.click_element(AdminCouponVouchertemplateLocator.disable_ok_button, doc)
        time.sleep(3)
        # 校验是否disabled
        self.wait_elevisble(AdminCouponVouchertemplateLocator.actions_button, doc)
        self.move_mouse_to_click_element(AdminCouponVouchertemplateLocator.actions_button, doc)
        self.wait_elevisble(AdminCouponVouchertemplateLocator.enable_button, doc)
        # 判断enable 按钮的是否亮显
        result = self.get_element(AdminCouponVouchertemplateLocator.enable_button_high_light, doc).get_attribute('aria-disabled')
        assert result == 'false'

    # enable voucher template
    def enable_voucher_template(self):
        # 先创建模板,并将模板disabled，保证有可enabled的模板
        self.disable_voucher_template()
        time.sleep(2)
        doc = 'enable voucher template'
        self.wait_elevisble(AdminCouponVouchertemplateLocator.enable_button, doc)
        self.click_element(AdminCouponVouchertemplateLocator.enable_button, doc)
        time.sleep(2)
        # 校验是否enable成功
        self.wait_elevisble(AdminCouponVouchertemplateLocator.actions_button, doc)
        self.move_mouse_to_click_element(AdminCouponVouchertemplateLocator.actions_button, doc)
        self.wait_elevisble(AdminCouponVouchertemplateLocator.enable_button, doc)
        # 判断enable 按钮的是否灰显
        result = self.get_element(AdminCouponVouchertemplateLocator.enable_button_not_high_light, doc).get_attribute('aria-disabled')
        assert result == 'true'

    # 筛选 coupon template
    def filter_voucher_template(self):
        # 先创建模板,并将模板disabled，保证有可enabled的模板
        self.disable_voucher_template()
        time.sleep(2)
        doc = '筛选 voucher template'
        # 筛选enabled的
        self.click_element(AdminCouponVouchertemplateLocator.status_filter_button, doc)
        time.sleep(2)
        # self.wait_elevisble(CouponCouponTemplateLocator.status_filter_enabled_button, doc)
        self.click_element(AdminCouponVouchertemplateLocator.status_filter_enabled_button, doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.status_filter_ok_button, doc)
        time.sleep(5)
        # 检查筛选结果, coupon template是disabled状态，没有筛选结果
        result = self.element_is_exist(AdminCouponVouchertemplateLocator.first_search_result)
        assert not result

        # 筛选enabled的
        doc = '筛选 disabled coupon template'
        self.click_element(AdminCouponVouchertemplateLocator.status_filter_button, doc)
        # 取消勾选enabled
        time.sleep(2)
        # self.wait_elevisble(CouponCouponTemplateLocator.status_filter_enabled_button, doc)
        self.click_element(AdminCouponVouchertemplateLocator.status_filter_enabled_button, doc)
        time.sleep(2)
        # 勾选disabled
        self.click_element(AdminCouponVouchertemplateLocator.status_filter_disabled_button, doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.status_filter_ok_button, doc)
        time.sleep(5)
        # 检查筛选结果, coupon template是disabled状态，有筛选结果
        result = self.element_is_exist(AdminCouponVouchertemplateLocator.first_search_result)
        assert result


    # 创建成功后，根据名称查询，校验列是否展示刚创建的template
    def check_create_voucher_template_result(self, voucher_template_name):
        doc = '验证创建voucher template是否成功'
        time.sleep(2)
        self.wait_elevisble(AdminCouponVouchertemplateLocator.search_input, doc)
        self.send_keys(AdminCouponVouchertemplateLocator.search_input, voucher_template_name, doc)
        time.sleep(2)
        self.click_element(AdminCouponVouchertemplateLocator.search_button, doc)
        time.sleep(5)
        assert self.get_element(AdminCouponVouchertemplateLocator.first_search_result, doc).text == voucher_template_name


    #校验列表属性
    # 校验列表元素
    # 时区，货币单位需要单独提出来，根据配置文件来校验  --待优化
    def check_list_attribute(self):
        time.sleep(2)
        doc = "校验列表元素"
        attribute_list = ['Voucher Template', 'Status', 'Fixed Amount', 'Reuse', 'Validation Period', 'Creator', 'Creation Time']
        list_attribute_0 = self.get_element(AdminCouponVouchertemplateLocator.voucher_template_list_voucher_template, doc).text
        self.check_contain_str(list_attribute_0, attribute_list[0])
        list_attribute_1 = self.get_element(AdminCouponVouchertemplateLocator.voucher_template_list_status, doc).text
        self.check_contain_str(list_attribute_1, attribute_list[1])
        list_attribute_2 = self.get_element(AdminCouponVouchertemplateLocator.voucher_template_list_fixed_amount, doc).text
        self.check_contain_str(list_attribute_2, attribute_list[2])
        list_attribute_3 = self.get_element(AdminCouponVouchertemplateLocator.voucher_template_list_reuse, doc).text
        self.check_contain_str(list_attribute_3, attribute_list[3])
        list_attribute_4 = self.get_element(AdminCouponVouchertemplateLocator.voucher_template_list_validation_period, doc).text
        self.check_contain_str(list_attribute_4, attribute_list[4])
        list_attribute_5 = self.get_element(AdminCouponVouchertemplateLocator.voucher_template_list_creator, doc).text
        self.check_contain_str(list_attribute_5, attribute_list[5])
        list_attribute_6 = self.get_element(AdminCouponVouchertemplateLocator.voucher_template_list_creation_time, doc).text
        self.check_contain_str(list_attribute_6, attribute_list[6])
        time.sleep(3)